/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.task;

import com.appspot.ittrick.drive.trick.Constants;
import com.appspot.ittrick.drive.trick.api.DriveAPIService;
import com.appspot.ittrick.drive.trick.controller.MainController;
import com.google.api.services.drive.model.File;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 *
 * @author takahara
 */
public class GoogleAddNewFileTask extends Service<File> {
    
    private File parentFile;
    private Object fileType;
    
    public void run(File parentFile, Object fileType) {
        this.parentFile = parentFile;
        this.fileType = fileType;
        this.restart();
    }

    @Override
    protected Task<File> createTask() {
        return new Task<File>() {

            @Override
            protected File call() throws Exception {
                DriveAPIService driveService = MainController.getInstance().getDriveService();
                File file = null;
                
                if (fileType == Constants.GOOGLE_FILE_TYPES_ENUM.docs) {
                    file = driveService.insertFile(
                            parentFile.getId(), 
                            Constants.GOOGLE_FILE_TYPES_ENUM.docs.getMineType(), 
                            "新しいドキュメント");

                } else if (fileType == Constants.GOOGLE_FILE_TYPES_ENUM.presentations) {
                    file = driveService.insertFile(
                            parentFile.getId(), 
                            Constants.GOOGLE_FILE_TYPES_ENUM.presentations.getMineType(), 
                            "新しいプレゼンテーション");

                } else if (fileType == Constants.GOOGLE_FILE_TYPES_ENUM.spreadsheets) {
                    file = driveService.insertFile(
                            parentFile.getId(), 
                            Constants.GOOGLE_FILE_TYPES_ENUM.spreadsheets.getMineType(), 
                            "新しいスプレッドシート");

                } else if (fileType == Constants.GOOGLE_FILE_TYPES_ENUM.forms) {
                    file = driveService.insertFile(
                            parentFile.getId(), 
                            Constants.GOOGLE_FILE_TYPES_ENUM.forms.getMineType(), 
                            "新しいフォーム");

                } else if (fileType == Constants.GOOGLE_FILE_TYPES_ENUM.drawings) {
                    file = driveService.insertFile(
                            parentFile.getId(), 
                            Constants.GOOGLE_FILE_TYPES_ENUM.drawings.getMineType(), 
                            "新しい図形");

                } else if (fileType == Constants.GOOGLE_FILE_TYPES_ENUM.folder) {
                    file = driveService.insertFolder(
                            parentFile.getId(), 
                            "新しいフォルダ");
                }
                
                return file;
            }
        };
        
    }
    
}
